// ignore_for_file: overridden_fields

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';

class CustomHttpException implements Exception {
  final String? _code;
  final String? _msg;

  String get code => _code ?? "-1";

  String get msg => _msg ?? runtimeType.toString();

  CustomHttpException([this._msg, this._code]);

  @override
  String toString() {
    if (msg == "") return "Exception";
    return "Exception: code $code, $msg";
  }

  static CustomHttpException parseException(DioException exception) {
    debugPrint("------err${exception.type}");
    switch (exception.type) {
      case DioExceptionType.connectionTimeout:
      case DioExceptionType.sendTimeout:
      case DioExceptionType.receiveTimeout:
        return NetworkException(msg: exception.message, code: "-1");
      case DioExceptionType.cancel:
        return CancelException(msg: exception.message);

      case DioExceptionType.badResponse:
        {
          try {
            String errCode = exception.response != null
                ? exception.response!.statusCode!.toString()
                : "-1";
            switch (errCode) {
              case "400":
                return BadRequestException(msg: "请求语法错误", code: errCode);
              case "401":
                return UnauthorisedException(msg: "没有权限", code: errCode);
              case "403":
                return BadRequestException(msg: "服务器拒绝执行", code: errCode);
              case "404":
                return BadRequestException(msg: "无法连接服务器", code: errCode);
              case "405":
                return BadRequestException(msg: "请求方法被禁止", code: errCode);
              case "500":
                return BadServiceException(msg: "服务器内部错误", code: errCode);
              case "502":
                return BadServiceException(msg: "无效的请求", code: errCode);
              case "503":
                return BadServiceException(msg: "服务器挂了", code: errCode);
              case "505":
                return UnauthorisedException(msg: "不支持HTTP协议请求", code: errCode);
              default:
                {
                  return UnknownException(
                      msg: exception.response != null
                          ? exception.response!.statusMessage!
                          : "",
                      code: errCode);
                }
            }
          } on Exception catch (_) {
            return UnknownException(msg: "未知错误", code: "-1");
          }
        }

      case DioExceptionType.connectionError:
        {
          return NetworkException(msg: "moment_moments_00029", code: "-1");
        }
      case DioExceptionType.unknown:
        if (exception.error is SocketException) {
          return NetworkException(msg: "moment_moments_00029", code: "-1");
        } else {
          return UnknownException(msg: exception.message, code: "-1");
        }
      default:
        {
          return UnknownException(msg: exception.message, code: "-1");
        }
    }
  }
}

/// 客户端异常
class BadRequestException extends CustomHttpException {
  BadRequestException({String? msg, String? code}) : super(msg, code);
}

/// 服务器异常
class BadServiceException extends CustomHttpException {
  BadServiceException({String? msg, String? code}) : super(msg, code);
}

/// 未知异常
class UnknownException extends CustomHttpException {
  UnknownException({String? msg, String? code}) : super(msg, code);
}

/// 取消异常
class CancelException extends CustomHttpException {
  CancelException({String? msg}) : super(msg);
}

/// 网络异常
class NetworkException extends CustomHttpException {
  NetworkException({String? msg, String? code}) : super(msg, code);
}

/// 401
class UnauthorisedException extends CustomHttpException {
  UnauthorisedException({String? code = "401", String? msg}) : super(msg);
}

class BadResponseException extends CustomHttpException {
  dynamic data;
  BadResponseException(String? msg, String? code, {this.data})
      : super(msg, code);
}
